/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.base.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.util.StringUtils;

import cn.com.ksplatform.base.dao.IBaseHibernateDAO;
import cn.com.ksplatform.base.dao.IBaseJdbcDAO;
import cn.com.ksplatform.base.page.PageInfoModel;
import cn.com.ksplatform.common.util.BaseDaoUtil;

/**
 * 业务层基类
 * 
 * BaseService
 * 
 * 2015年3月17日 上午9:41:15
 * 
 * @author Mr.Xiao
 * @version 1.0.0 <T extends BaseModel>
 */

@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class BaseService {

	protected IBaseHibernateDAO hibernateDAO;
	
	protected IBaseJdbcDAO jdbcDAO;

	@Resource(name = "baseHibernateDao")
	public void setHibernateDAO(IBaseHibernateDAO hibernateDAO) {
		this.hibernateDAO = hibernateDAO;
	}

	@Resource(name = "baseJdbcDao")
	public void setJdbcDAO(IBaseJdbcDAO jdbcDAO) {
		this.jdbcDAO = jdbcDAO;
	}
	@Transactional
	public Object uniqueResult(Class clazz,String propertyName, Object value){
		return this.hibernateDAO.uniqueResult(clazz, propertyName,value);
	}
	@Transactional
	public Object get(Class clazz,Serializable id){
		return this.hibernateDAO.get(clazz, id);
	}
	@Transactional
	public Object load(Class clazz,Serializable id){
		return this.hibernateDAO.load(clazz, id);
	}
	@Transactional
	public Object findAll(final String hql){
		return this.hibernateDAO.findAll(hql);
	}
	@Transactional
	public Object findAll(final String hql,List param){
		return this.hibernateDAO.findAll(hql,param);
	}
	@Transactional
	public Object findAll(final Class clazz,Map orderMap){
		String hql = "  from " + clazz.getName() + " where " + BaseDaoUtil.getPKName(clazz) + " is not null";
		if (orderMap!=null&&orderMap.get("sortName")!=null) {
			hql+=" order by "+orderMap.get("sortName");
			if (orderMap.get("sortValue")!=null) {
				hql+=" "+orderMap.get("sortValue");
			}
		}
		return this.hibernateDAO.findAll(hql);
	}
	@Transactional
	public Object findAll(final Class clazz,final Map m,Map orderMap){
		String hql = "  from " + clazz.getName() + " where " + BaseDaoUtil.getPKName(clazz) + " is not null";
		
		if(m != null){
			hql+=(" and "+m.get("query"));
		}
		
		
		if (orderMap!=null&&orderMap.get("sortName")!=null) {
			hql+=" order by "+orderMap.get("sortName");
			if (orderMap.get("sortValue")!=null) {
				hql+=" "+orderMap.get("sortValue");
			}
		}
		if(m != null){
			List<Object> params = (List<Object>) m.get("params");
			Object result = this.hibernateDAO.findAll(hql,params);
			return result;
		}else{
			return this.hibernateDAO.findAll(hql);
		}
		
	}
	/**
	 * 
	 * queryPage:hql翻页. <br/>
	 * 
	 * @author Mr.Luoxing
	 * @param pageInfo
	 * @param clazz
	 * @param values
	 * @return
	 */
	@Transactional
	public PageInfoModel queryPage(PageInfoModel pageInfo, Class clazz,Map m) {
		
		String hql = "  from " + clazz.getName() + " where " + BaseDaoUtil.getPKName(clazz) + " is not null  ";
		StringBuffer sb = new StringBuffer();
		if(m!=null){
			String whereSql = m.get("query").toString();
			if(StringUtils.hasText(whereSql)){
				hql += (" and "+whereSql);
			}
			List params = (List)m.get("params");
			hql = hql + sb.toString();
			return this.hibernateDAO.queryPage(hql, pageInfo, params);
		}else{
			return this.hibernateDAO.queryPage(hql, pageInfo);
		}
		
	}

	public IBaseHibernateDAO getHibernateDAO() {
		return hibernateDAO;
	}

	public IBaseJdbcDAO getJdbcDAO() {
		return jdbcDAO;
	}

}
